package nl.infodation.emr.pricingservice.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import nl.infodation.emr.pricingservice.dao.SystemValueRoleDAO;
import nl.infodation.emr.pricingservice.entity.SystemValueRole;

import org.hibernate.QueryException;

public class SystemValueRoleDAOImpl extends DAOImpl<SystemValueRole, Integer>
		implements SystemValueRoleDAO {

	private static SystemValueRoleDAO instance;
	
	private SystemValueRoleDAOImpl() { }
	
	public static synchronized SystemValueRoleDAO getInstance() {
		if (instance == null) {
			instance = new SystemValueRoleDAOImpl();
		}
		return instance;
	}

	/* (non-Javadoc)
	 * @see nl.infodation.emr.pricingservice.dao.SystemValueRoleDAO#findIDsByRole(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> findIDsByRole(String roleName) {
		List<Integer> result = new ArrayList<>();	

		EntityManager entityManager = null;
		try {
			entityManager = createEntityManager(EM_FACTORY);
						
			Query query = entityManager.createQuery(FIND_SYTEM_VALUE_IDS_BY_ROLE);
			query.setParameter("roleName", roleName);
			result = query.getResultList();
		} catch (IllegalArgumentException | QueryException ex) {
			LOG.error("Error reading findIDsByRole.", ex);
		} finally {
			close(entityManager);
		}
		
		return result;
	}
	
	

}
